import http from '../../../../services/http/index'

import routes from '../../../../routes/index'
import { areaCode as areaCodeRange } from '../../../../computed/index'

const MAP = {
  vcodeBtn: {
    normal: '获取验证码',
    loading: '{sec} 秒后重发',
  }
}
let util = require('../../../../utils/index')
var interval = null //倒计时函数

Page({
  /**
   * 页面的初始数据
   */
  data: {
    time: '获取验证码', //倒计时
    currentTime: 61,
    mobile: '',
    myPhone:'',
    myAreaCode:'',
    // 区号选择器
    areaCodePicker: {
      range: areaCodeRange,
      value: 1,
    },
    // 获取验证码按钮
    vcodeBtn: {
      text: MAP.vcodeBtn.normal,
      disabled: false,
    },

    // 确认按钮
    confirmBtn: {
      disabled: true,
    }
  },
  onLoad: function (options) {
  },
  /**
   * picker change 事件处理函数
   * @param event
   * @returns {void}
   */
  handlePickerChange({ detail: { value } }) {
    this.setData({ 'areaCodePicker.value': value })
  },
  
  /**
   * input 输入事件处理函数
   * @param event
   * @returns {void}
   */
  // 判断手机号和验证码的输入框是否为空-好讲下面的的“确定”设为高亮
  handleInputChange({ currentTarget: { dataset: { name } }, detail: { value } }) {
    this.setData({ [name]: value })
    console.log(value);
    this.setData({
      myPhone: value
    })
    console.log(this.data.myPhone)
    if (this.data.myPhone != '' && this.data.myAreaCode !='' ){
      this.setData({
        'confirmBtn.disabled': false
      })
    }
  },
  // 判断手机号和验证码的输入框是否为空-好讲下面的的“确定”设为高亮
  handleInputChange2({ currentTarget: { dataset: { name } }, detail: { value } }) {
    this.setData({ [name]: value })
    console.log(value);
    this.setData({
      myAreaCode: value
    })
    console.log(this.data.myAreaCode)
    if (this.data.myPhone != '' && this.data.myAreaCode != '') {
      this.setData({
        'confirmBtn.disabled': false
      })
    }
  },

  /**
   * 获取验证码按钮点击处理函数
   * @returns {void}
   */
  handleVcodeBtnTap() {
    //验证正确输入手机号之后发送验证码
    var mobile = this.data.mobile;
    var picker = this.data.areaCodePicker;

    var areaCodeMobile = picker.range[picker.value].areaCode == '+86' ? mobile: picker.range[picker.value].areaCode +mobile;
    var reg_au = /^(\+?61|0)4\d{8}$/,reg_ch = /^1[3456789]\d{9}$/,reg_nz=/^(\+?64|0)2\d{7,9}$/;
    var regMobile = false;
    if(reg_au.test(areaCodeMobile) || reg_ch.test(areaCodeMobile) || reg_nz.test(areaCodeMobile)){
      regMobile = true
    }
    console.log(areaCodeMobile,regMobile)

    if (mobile == '') {
      const error = '请输入手机号！';
      wx.showToast({
        title: `${error}`,
        icon: 'none'
      });
      return false
    }
    if (!regMobile) {
      const error = '请输入正确的手机号!';
      wx.showToast({
        title: `${error}`,
        icon: 'none'
      });
      return false
    }else{
      var that = this;
      //获取区号和手机号
      var { areaCodePicker, mobile, vcodeBtn } = that.data
      if (vcodeBtn.disabled) return
      var areaCode = areaCodePicker.range[areaCodePicker.value].areaCode;

      var mobile = that.data.mobile;

      //请求接口-发送验证码
      http.sendVcode({
        areaCode: areaCode,
        mobile: mobile,
        type:3,
      }).then(res => {
        console.log('成功')
        console.log(res)
        this.getCode();
        var that = this
        that.setData({
          disabled: true
        })
      }).catch(function (err) {
        console.log(err)
        wx.showToast({
          title: err.message,
          icon: 'none',
          duration: 2000
        })
      })
    }
  },
  getCode: function (options) {
    var that = this;
    var currentTime = that.data.currentTime
    interval = setInterval(function () {
      currentTime--;
      that.setData({
        time: currentTime + '秒'
      })
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: '重新发送',
          currentTime: 61,
          disabled: false
        })
      }
    }, 1000)
   
  },


  /**
   * 确认按钮点击处理函数
   * @returns {void}
   */
  handleConfirmBtnTap() {
    var that=this;
    if (this.data.confirmBtn.disabled){
      return;
    } else{
      var { areaCodePicker, mobile, vcodeBtn } = that.data
      if (vcodeBtn.disabled) return
      var areaCode = areaCodePicker.range[areaCodePicker.value].areaCode;
      // 验证验证码是否正确
      //请求接口-验证验证码
      http.sendForgetYzm({
        areaCode: areaCode,
        mobile: that.data.myPhone,
        code: this.data.myAreaCode
      }).then(data => {
        // 成功后跳到下一个页面
        wx.showToast({
          title: '注册成功',
          icon: 'success',
          duration: 2000
        });
        wx.navigateTo({ url: `../step2/forget2?mobile=${that.data.myPhone}&areaCode=${areaCode}&code=${this.data.myAreaCode}`})

        // 把注册的电话号码存下来
        console.log('注册成功信息')
        console.log(data)
      }).catch(function(err){
        console.log(err)
        wx.showToast({
          title: err.message,
          icon: 'none',
          duration: 2000
        })
      })

    }
    
    // wx.navigateTo({ url: routes.RegisterStep2 })
  }
})